package com.tarena.lbs.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.common.enums.Roles;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.common.utils.AuthenticationContextUtils;
import com.tarena.lbs.user.dao.repository.AdminRepository;
import com.tarena.lbs.user.pojo.param.AdminParam;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.query.AdminQuery;
import com.tarena.lbs.user.pojo.vo.AdminVO;
import com.tarena.lbs.user.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AdminServiceImpl implements AdminService {
    @Autowired
    AdminRepository adminRepository;

    @Override
    public AdminVO detail(Integer id) {
        log.debug("查询用户详情,service层");
        AdminPO  adminPO=adminRepository.getById(id);
        AdminVO adminVO=new AdminVO();
        BeanUtils.copyProperties(adminPO,adminVO);
        return adminVO;
    }

    @Override
    public PageResult<AdminVO> pageList(AdminQuery query) {
        log.debug("查询账号列表,service层");
        //1.准备一个 返回值 PageResult
        PageResult<AdminVO> voPage=new PageResult<>(query.getPageSize(),query.getPageNo());
        //2.给pageResult封装4个属性 pageNo pageSize 入参 total list 分页查询查询 pageInfo 转化vo
        //2.1调用仓储层 获取pageInfo
        PageInfo<AdminPO> pageInfo= getPageAdmin(query);
        //2.2将pageInfo的total交给pageResult使用
        voPage.setTotal(pageInfo.getTotal());
        //2.3 判断非空 在转化vos
        List<AdminPO> pos = pageInfo.getList();
        List<AdminVO> vos=null;
        if (pos!=null&& pos.size()>0){
            vos=pos.stream().map(po->{
                AdminVO vo=new AdminVO();
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        voPage.setObjects(vos);
        return voPage;
    }

    private PageInfo<AdminPO> getPageAdmin(AdminQuery query) {
        //应该使用query中 accountPhone和nickname 业务参数拼接查询条件
        //开启分页
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        //理解查询列表
        List<AdminPO> pos = adminRepository.list();//没有参数查询所有
        //封装pageInfo返回
        return new PageInfo<>(pos);
    }

    @Override
    public void save(AdminParam param) throws BusinessException {
        log.debug("账号保存,service层");
//      checkRole(Roles.ADMIN);
        saveAdmin(param);
    }

    private void saveAdmin(AdminParam param) throws BusinessException {
        //1.查询入参电话号码是否存在 select count(*) from lbs_admin where account_phone=?
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("account_phone",param.getAccountPhone());
        log.debug("查询手机号数量,service层{}",queryWrapper);
        Long count=adminRepository.count(queryWrapper);
        Asserts.isTrue(count>0, new BusinessException("-1","手机号已存在"));
        Asserts.isTrue(param.getAccountPhone().length()!=11,new BusinessException("-1","手机号长度11位"));
        log.debug("手机号数量{}",count);
        //2.不存在 则转化po新增
        AdminPO adminPO=new AdminPO();
        BeanUtils.copyProperties(param,adminPO);
        adminPO.setCreateTime(new Date());
        log.debug("转化po{}",adminPO);
        adminRepository.save(adminPO);
    }


}
